if (!jsx3.lang.Class.forName("ux.cdb.admin.queries.student.Query")) {
    
jsx3.Class.defineClass(
    "ux.cdb.admin.queries.student.Query", jsx3.gui.Block, [],
    function (query) {

		/**
		 * Helper Functions, return a copy function with 
		 * @param fn {Function}
		 * @param scope {Object} 
		 */
		query.prototype.bind = function(fn, scope) {
			scope = scope || this;
			return function() {
				fn.apply(scope, arguments);
			};
		};
		
		/**
		 * Add event to jsx Object
		 * @param name {String} jsx object name or comma-splitted names
		 * @param event {String} event name
		 * @param fct {Function} event handler
		 */
		query.prototype.on = function (name, event, fct) {
			var names = name.split(","), block=this;
			for(var i=0; i<names.length; i++) {
				block = block.get(names[i]);
			}
			block[event] = this.bind(fct);
		};

		query.prototype.initialize = function(){
		};
		
		query.prototype._initialize = function(){
/* @JSC :: begin DEBUG */
			jsx3.log('student#Query#initialize');
/* @JSC :: end */
			var me = this;
			this.queryUtil = ux.cdb.admin.queries.QueryUtil;
			this.on("btnQuery", "onClick", me.onQuery);
			this.on("btnReset", "onClick", me.onReset);
			this.queryDetail = this.get('queryDetail');
			this.matrix = this.get('mtxMain');
			ux.cdb.admin.CommonCache.loadCache(['INTERNAL_ORG'],me,
				function(){
					var _cache = ux.cdb.admin.CommonCache.getInstance();
					me.get('blkFieldinternalOrgId').getChild(1).setSourceXML(_cache.getCache('CommonCache_INTERNAL_ORG'));
					me.get('blkFieldinternalOrgId').getChild(1).repaint();
					me.get('blkFieldcooperOrgId').getChild(1).setSourceXML(jsx3.xml.CDF.Document.newDocument().insertRecord(this.queryUtil.getDefaultOption()));
					me.get('blkFieldcooperOrgId').getChild(1).repaint();
				});

			this.pageSize = 20;
			this.totalRecordCount = 100;
			this.currentPage = 1;
			this.totalPages = 1; 
			this.pageLoaded = false;
			this.pagebar = this.get("blkPageWheel");
		};
		
		query.prototype.displayTip = function(passed) {
			if(!passed) {
				var tip = ux.megatron.Main.getServer().getDynamicProperty("@cdb@queries@TipUnFinished");
				this.get('_messageLable').setText(tip,true);
			} else {
				this.get('_messageLable').setText("",true);
			}
		};
		
		query.prototype.onQuery = function(){
			if(this.queryDetail.doValidate() !== null){
				this.displayTip(false);
				return;
			}else{
				this.displayTip(true);
			}
			var _data = this.queryDetail.getData();
			var _params = [
							_data.getFirstChild().getAttribute('internalOrgId'),
							_data.getFirstChild().getAttribute('cooperOrgId'),
							_data.getFirstChild().getAttribute('studentName'),
							_data.getFirstChild().getAttribute('certCode'),
							this.currentPage, //current page
							this.pageSize //page size
						  ];

/* @JSC :: begin DEBUG */
			jsx3.log('query data : ' + _data + ' | params : ' + _params);
/* @JSC :: end */
			ux.megatron.util.Dwr.callDwr("RetailInfoQueryUCC","getStudentListByCondition",_params,this,
				function(data){
					if (data != null && typeof data == 'object') {
/* @JSC :: begin DEBUG */
						jsx3.log('Query#doQuery data : ' + dwr.util.toDescriptiveString(data, 5));
/* @JSC :: end */
						this.setMatrixData(data);
					}
				});
		};

		query.prototype.setMatrixData = function(data) {
			this.loadPageControler(data.content.page.count); 
			data = data.content.data;
			if(data && data instanceof Array) {
				var tmpCDF = jsx3.xml.CDF.Document.newDocument();
				for(var i=0,item;item=data[i];i++){
					tmpCDF.insertRecord(item,null,false).setAttribute('jsxid',(i+1));
				}
/* @JSC :: begin DEBUG */
				jsx3.log("Query#setMatrixData : cdf= " + tmpCDF);
/* @JSC :: end */
				this.matrix.setSourceXML(tmpCDF);
				this.matrix.repaintData();
			} else {
				jsx3.log('Query#setMatrixData cdf data of matrix:' + data);
			}
		};
		
		query.prototype.loadPageControler = function(count) {
			this.totalRecordCount = count;
			this.totalPages = (count%this.pageSize>0) ? (parseInt(count/this.pageSize, 10) + 1) : (count/this.pageSize);
			if(!this.pageLoaded){
				var me = this;
				this.pagebar.initialize();
				this.pagebar.bindWithMatrix(me,me.onPageChange,me.matrix);
				this.pageLoaded = true;
			}
			this.pagebar.setValue(this.currentPage, this.totalPages, this.pageSize, this.totalRecordCount);
		};
		
		query.prototype.onPageChange = function(msg) {
			this.currentPage = msg.currentPageNumber;
			this.onQuery();
		};
				
		query.prototype.onReset = function(){
			this.queryDetail.setDataByObject({internalOrgId:'',cooperOrgId:'',studentName:null,certCode:null});
			this.queryDetail.repaint();
/* @JSC :: begin DEBUG */
			jsx3.log('student#Reset#initialize');
/* @JSC :: end */
		};
		
		query.prototype.loadCooperOrgList = function(recordId){
			if(!recordId) return;
			var cooperOrgBox = this.get('blkFieldcooperOrgId').getChild(1);
			cooperOrgBox.clearXmlData();
			cooperOrgBox.setSourceXML(jsx3.xml.CDF.Document.newDocument().insertRecord({jsxid:'',jsxtext:'Loading...',jsxstyle:'width:100%;'}));
			cooperOrgBox.repaint();
			var option = [{
				dwrClass:'RetailInfoQueryUCC',
				dwrFunction:'getInitCooperOrg',
				dwrParams:[recordId]
			}];
			this.queryUtil.loadCustomizeCache(option, this, function(){
				var cdf = this.queryUtil.getInstance().getCacheAsCDF(0, {jsxid:'partyId', jsxtext:'chnFullName'});
				cooperOrgBox.setSourceXML(cdf);
				cooperOrgBox.repaint();
/* @JSC :: begin DEBUG */
				jsx3.log('loadCustomizeCache finished. cdf : ' + cdf);
/* @JSC :: end */
			});
/* @JSC :: begin DEBUG */
			jsx3.log('loadCooperOrgList recordId : ' + recordId);
/* @JSC :: end */
		};
	
		query.prototype.matrixSelected = function(mtx,strRECORDID){
			var parent = ux.megatron.Main.getServer().getBodyBlock();
			var me = this;
			var rec = mtx.getRecord(strRECORDID);
			ux.megatron.Main.loadUIResourceAsync(parent,"ux.cdb.admin","ux/cdb/admin/queries/student/ui/detaildialog.xml",me, me.dialogLoaded);
		};
		
		query.prototype.dialogLoaded = function(objEvent){
/* @JSC :: begin DEBUG */
			console.log('Customer#Student detailDialog loaded. response objEvent:\n',objEvent);
/* @JSC :: end */
		};
		
    });
}